package com.example.controller;

import com.example.common.Result;
import com.example.entity.Article;
import com.example.service.ArticleService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    //新增数据
    @PostMapping("/add")
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        return  Result.success();
    }

    //更新数据
    @PutMapping("/update")
    public Result update(@RequestBody Article article) {
        articleService.update(article);
        return  Result.success();
    }

    //删除单个数据
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        articleService.deleteById(id);
        return  Result.success();
    }

    //批量删除数据
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) //数组用@RequestBod接收
    {
        articleService.deleteBatch(ids);
        return  Result.success();
    }

    //根据id查询
    @GetMapping("/selectById/{id}")   // http://localhost:9090/article/selectById/1/101
    public Result selectById(@PathVariable Integer id) {
        Article article = articleService.selectById(id);
        return  Result.success(article);
    }

//查询所有的数据
    @GetMapping("/selectAll")   // /article/selectAll
    public Result selectAll(Article article) {
        List<Article> list = articleService.selectAll(article);
        return  Result.success(list);
    }

    //分页查询数据
    /*
    pageNum:当前页码；pageSize：每页多少个
     */
    @GetMapping("/selectPage")   // /article/selectAll
    public Result selectPage(Article article,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Article> pageInfo = articleService.selectPage(article, pageNum, pageSize);
        return  Result.success(pageInfo);
    }



//    @GetMapping("/selectOne")   // http://localhost:9090/article/selectOne?id=4&no=104
//    public Result selectOne(@RequestParam Integer id, @RequestParam (required = false) String no) {
//        Article article = articleService.selectById(id);
//        return  Result.success(article);
//    }
//
//    @GetMapping("/selectList")   // http://localhost:9090/article/selectOne?id=4&no=104
//    public Result selectList(Article article) {
//        List<Article> List = articleService.selectList(article);
//        return  Result.success(List);
//    }
}
